﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Net;

namespace Watcher
{
    class ClassTab : TabPage
    {
        public int CharID = 0;

        TabControl Chats;
        ContextMenu m_con;
        Timer t1;

        // MENUPart
        MenuItem CGroup; // Create Group
        MenuItem LGroup; // Leave Group

        MenuItem LGuild; // Leave guild

        MenuItem LBattleground; // Leave BG

        public ClassTab(string text, int CharId, string Faction, string iszRealm, string charinfo)
        {
            this.Text = text;
            this.CharID = CharId;
            this.Name = text;

            Chats = new TabControl();
            Chats.Name = "Chats";

            Chats.Parent = this;
            Chats.Size = new Size(690, 230);
            Chats.Location = new Point(10, 100);

            Label lvl = new Label();
            lvl.Name = "Lvl";
            lvl.Text = "";
            lvl.Size = new Size(100, 13);

            Label Charinfo = new Label();
            Charinfo.Name = "Charinfo";
            Charinfo.Text = charinfo;
            Charinfo.Size = new Size(250, 13);

            Label classinfo = new Label();
            classinfo.Name = "classinfo";
            classinfo.Text = "";
            classinfo.Size = new Size(200, 13);

            ProgressBar xp = new ProgressBar();
            xp.Name = "XpBar";
            xp.Size = new Size(100, 17);
            

            PictureBox pic = new PictureBox();
            pic.Name = "logo";
            pic.Location = new Point(0, 0);
            pic.Size = new Size(100, 100);
            pic.SizeMode = PictureBoxSizeMode.StretchImage;
            pic.MouseClick += new MouseEventHandler(pic_MouseClick);

            Label szRealm = new Label();
            szRealm.Name = "Realm";
            szRealm.Text = "Realm:             " + iszRealm;
            szRealm.Size = new Size(250, 13);

            Label szCurrentZone = new Label();
            szCurrentZone.Name = "CurrentZone";
            szCurrentZone.Size = new Size(250, 13);

            Label bag = new Label();
            bag.Name = "bag";
            bag.Text = "Bagslots:          0 / 0";
            bag.Size = new Size(250, 13);

            //Locations 

            Charinfo.Location = new Point(100, 10);
            classinfo.Location = new Point(100, 30);
            lvl.Location = new Point(100, 50);
            xp.Location = new Point(105, 70);

            szRealm.Location = new Point(350, 10);
            szCurrentZone.Location = new Point(350, 30);
            bag.Location = new Point(350, 50);


            //Create the Menu
            m_con = new ContextMenu();

            MenuItem Questlog = new MenuItem();
            Questlog.Name = "Questlog";
            Questlog.Text = "Questlog";
            Questlog.Click += new EventHandler(Questlog_Click);

            MenuItem Emotes = new MenuItem();
            Emotes.Name = "Emotes";
            Emotes.Text = "Emotes";
            Emotes.Click += new EventHandler(Emotes_Click);

            MenuItem Achievements = new MenuItem();
            Achievements.Name = "Achievements";
            Achievements.Text = "Achievements";
            Achievements.Click += new EventHandler(Achievements_Click);

            MenuItem Manage = new MenuItem();
            Manage.Name = "Manage";
            Manage.Text = "Manage";

            MenuItem showMap = new MenuItem();
            showMap.Name = "ShowMap";
            showMap.Text = "ShowMap";
            showMap.Click += new EventHandler(showMap_Click);

            // GRUPPEN MENU
            MenuItem Group = new MenuItem();
            Group.Name = "Group";
            Group.Text = "Group";

            CGroup = new MenuItem();
            CGroup.Name = "CGroup";
            CGroup.Text = "Create Group";
            CGroup.Click += new EventHandler(CGroup_Click);

            LGroup = new MenuItem();
            LGroup.Name = "LGroup";
            LGroup.Text = "Leave Group";
            LGroup.Click += new EventHandler(LGroup_Click);

            Group.MenuItems.Add(CGroup);
            Group.MenuItems.Add(LGroup);

            // GUILD MENU
            MenuItem Guild = new MenuItem();
            Guild.Name = "Guild";
            Guild.Text = "Guild";

            LGuild = new MenuItem();
            LGuild.Name = "LGuild";
            LGuild.Text = "Leave Guild";
            LGuild.Click += new EventHandler(LGuild_Click);

            Guild.MenuItems.Add(LGuild);


            // Battleground Menu
            MenuItem Battleground = new MenuItem();
            Battleground.Name = "Battleground";
            Battleground.Text = "Battleground";


            LBattleground = new MenuItem();
            LBattleground.Name = "LBattleground";
            LBattleground.Text = "Leave Battleground";
            LBattleground.Click += new EventHandler(LBattleground_Click);

            Battleground.MenuItems.Add(LBattleground);


            // Whisper Menu
            MenuItem Whisper = new MenuItem();
            Whisper.Name = "Whisper";
            Whisper.Text = "Whisper";

            MenuItem NWhisper = new MenuItem();
            NWhisper.Name = "NWhisper";
            NWhisper.Text = "New Whisperchat";
            NWhisper.Click += new EventHandler(NWhisper_Click);

            Whisper.MenuItems.Add(NWhisper);

            // Ignore Menu
            MenuItem Ignore = new MenuItem();
            Ignore.Name = "Ignore";
            Ignore.Text = "Ignore";
            Ignore.Click += new EventHandler(Ignore_Click);

            // ADD it to the Parentmenu
            Manage.MenuItems.Add(Group);
            Manage.MenuItems.Add(Guild);
            Manage.MenuItems.Add(Battleground);
            Manage.MenuItems.Add(Whisper);
            Manage.MenuItems.Add(Ignore);

            m_con.MenuItems.Add(Questlog);
            m_con.MenuItems.Add(Achievements);
            m_con.MenuItems.Add(Emotes);
            m_con.MenuItems.Add(Manage);
            m_con.MenuItems.Add(showMap);


            if (Faction.Equals("Horde"))
            {
                pic.LoadAsync(Application.StartupPath + "\\Images\\lh.png");
                lvl.ForeColor = Color.Red;
                classinfo.ForeColor = Color.Red;
                szRealm.ForeColor = Color.Red;
                szCurrentZone.ForeColor = Color.Red;
                Charinfo.ForeColor = Color.Red;
                bag.ForeColor = Color.Red;
            }
            else
            {
                pic.LoadAsync(Application.StartupPath + "\\Images\\la.png");
                lvl.ForeColor = Color.Blue;
                classinfo.ForeColor = Color.Blue;
                szRealm.ForeColor = Color.Blue;
                szCurrentZone.ForeColor = Color.Blue;
                Charinfo.ForeColor = Color.Blue;
                bag.ForeColor = Color.Blue;
            }
           


            this.Controls.Add(lvl);
            this.Controls.Add(xp);
            this.Controls.Add(classinfo);
            this.Controls.Add(pic);
            this.Controls.Add(szRealm);
            this.Controls.Add(szCurrentZone);
            this.Controls.Add(Charinfo);
            this.Controls.Add(bag);

            PictureBox RacePic = new PictureBox();
            /*switch(
            RacePic.Image = Image.FromFile(Application.StartupPath + "\\Images\\none.png")*/

           
            // Create The Tabs
            ChatTab say = new ChatTab("Say","SAY", true);
            ChatTab yell = new ChatTab("Yell's","YELL", true);
            ChatTab lotting = new ChatTab("Loots", "LOOTS", false);
           

            // add it to the TabControl
            Chats.TabPages.Add(say);
            Chats.TabPages.Add(yell);
            Chats.TabPages.Add(lotting);

            ImageList iconsList  = new ImageList();
            iconsList.Images.Add(Image.FromFile(Application.StartupPath + "\\Images\\none.png"));
            iconsList.Images.Add(Image.FromFile(Application.StartupPath + "\\Images\\new.png"));

            Chats.SelectedIndexChanged += new EventHandler(Chats_SelectedIndexChanged);
            Chats.KeyDown += new KeyEventHandler(Chats_KeyDown);
            Chats.ImageList = iconsList;

            
            t1 = new Timer(); // Timer anlegen
            t1.Interval = 5000; 
            t1.Tick+=new EventHandler(t1_Tick); // Eventhandler ezeugen der beim Timerablauf aufgerufen wird
            t1.Start(); // Timer starten


        }
        private int _ZoneId = 0;
        private int _level = 0; 
        private int _x = 0;
        private int _y = 0;

        public void SetCurrentCoords(int ZoneId, int level, int x, int y)
        {
            _ZoneId = ZoneId;
            _level = level;
            _x = x;
            _y = y;
        }

        void showMap_Click(object sender, EventArgs e)
        {
            Form1 f = (Form1)this.Parent.Parent;
           

            ShowMap sm = new ShowMap(_ZoneId, _level, _x, _y);
            sm.ShowDialog();

            sm.Dispose();

        }

        void Ignore_Click(object sender, EventArgs e)
        {
            NameInput ni = new NameInput();
            if (ni.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ni.text_player.Text))
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.IgnorePlayer(CharID, ni.text_player.Text);
                    MessageBox.Show("Player put on ignore", "Info");
                }
            }

            ni.Dispose();
        }

        void NWhisper_Click(object sender, EventArgs e)
        {
            NameInput ni = new NameInput();
            if (ni.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ni.text_player.Text))
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.IgnorePlayer(CharID, ni.text_player.Text);
                    ChatTab say = new ChatTab(ni.text_player.Text, "WHISPER", true, ni.text_player.Text);
                    Chats.TabPages.Add(say);
                }
            }

            ni.Dispose();
        }

        void LBattleground_Click(object sender, EventArgs e)
        {
           
        }

       

        void LGuild_Click(object sender, EventArgs e)
        {
            Form1 f = (Form1)this.Parent.Parent;
            f.LeaveGuild(CharID);
            RemoveChatTab("Guild");
            HandleGuildMenu(false);
            MessageBox.Show("Guild left", "Info");
        }

   
        void Achievements_Click(object sender, EventArgs e)
        {
            
        }

        void Emotes_Click(object sender, EventArgs e)
        {
            
        }

        public void HandleGroupMenu(bool enable)
        {
            if (!enable)
            {
                CGroup.Enabled = true;
                LGroup.Enabled = false;
            }
            else
            {
                CGroup.Enabled = false;
                LGroup.Enabled = true;
            }
        }

        public void HandleGuildMenu(bool enable)
        {
            if (!enable)
            {
                LGuild.Enabled = false;
            }
            else
            {

                LGuild.Enabled = true;
            }
        }

        void LGroup_Click(object sender, EventArgs e)
        {
            Form1 f = (Form1)this.Parent.Parent;
            f.LeaveGroup(CharID);
            RemoveChatTab("Party");
            HandleGroupMenu(false);
                          
            MessageBox.Show("Group left", "Info");
        }

        void CGroup_Click(object sender, EventArgs e)
        {
            NameInput ni = new NameInput();
            if(ni.ShowDialog() == DialogResult.OK)
            {
                if(!string.IsNullOrEmpty(ni.text_player.Text))
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.AddGroupInvite(CharID, ni.text_player.Text);
                    MessageBox.Show("Invite send", "Info");
                }
            }

            ni.Dispose();
           
        }

        bool GroupInvite = false;

        public void AskForGroupInvite(string name)
        {

            if (!GroupInvite)
            {
                GroupInvite = true;

                if (MessageBox.Show(name + " has invited you in group! Accept invite ?", "Groupinvite", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.AcceptGroupInvite(CharID);
                    GroupInvite = false;
                }
                else
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.DeclineGroupInvite(CharID);
                    GroupInvite = false;
                }
            }
        }

        bool GuildInvite = false;

        public void AskForGuildInvite(string name, string guild)
        {

            if (!GuildInvite)
            {
                GuildInvite = true;

                if (MessageBox.Show(name + " has invited you in Guild " + guild + "! Accept invite ?", "Groupinvite", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.AcceptGuildInvite(CharID);
                    GuildInvite = false;
                }
                else
                {
                    Form1 f = (Form1)this.Parent.Parent;
                    f.AcceptGuildInvite(CharID);
                    GuildInvite = false;
                }
            }
        }
        
        void Questlog_Click(object sender, EventArgs e)
        {
            
        }

        void pic_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {

                this.m_con.Show(this, e.Location);

            }
        }

        public void SetAliveStatus(Boolean alive)
        {

        }

        public void SetBagStat(string baginfo)
        {
            this.Controls["bag"].Text = "Bagslots:          " + baginfo;
        }

        public void SetCurrentZone(string currentZone)
        {
            this.Controls["CurrentZone"].Text = "Current zone:   " + currentZone;
        }

        public void SetCurrentGold(int Amount)
        {

            //this.Controls["goldInfod"].Text = "Current gold:    " + baginfo;
        }

        void Chats_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                string TabName = Chats.SelectedTab.Name;

                if (TabName != "Guild" && TabName != "Say" && TabName != "Yell's")
                {
                    string message = "Do you really want to delete the tabpage \"" + TabName + "\"?";
                    string caption = "Confirmation";
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result;

                    // Displays the MessageBox.

                    result = MessageBox.Show(message, caption, buttons);

                    if(result == DialogResult.Yes)
                    {

                        Chats.TabPages.Remove(Chats.TabPages[TabName]);
                        GC.Collect();
                    }

                 
                }
            }
        }
        
        void Chats_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Chats.SelectedTab.ImageIndex != 0)
            {
                Chats.SelectedTab.ImageIndex = 0;
            }
        }

        public void RemoveChatTab(string ChatTabeName)
        {
            bool bound = false;
            for(int i = 0; i < Chats.TabPages.Count; i++)
            {
                if (Chats.TabPages[i].Name.Equals(ChatTabeName))
                    bound = true;
            }
           
            if(bound)
                Chats.TabPages.Remove(Chats.TabPages[ChatTabeName]);
        }

        public void CreateChatTab(string ChatTabeName, string ChatType, bool textchat)
        {
            bool bound = false;
            for(int i = 0; i < Chats.TabPages.Count; i++)
            {
                if (Chats.TabPages[i].Name.Equals(ChatTabeName))
                    bound = true;
            }

            if (!bound)
            {
                ChatTab newChat = new ChatTab(ChatTabeName, ChatType, textchat);
                Chats.TabPages.Add(newChat);
            }
  
        }
        public void CreateChatTab(string ChatTabeName, string ChatType)
        {
            ChatTab Chat = new ChatTab(ChatTabeName, ChatType, true, ChatTabeName);
            Chats.Controls.Add(Chat);
            Chats.SelectedTab.ImageIndex = 0;
  
        }
        

        private void t1_Tick(object sender, EventArgs e)
        {
            try
            {
                Form1 f = (Form1)this.Parent.Parent;
                f.Update((ClassTab)this);
            }
            catch (Exception ex)
            {

            }
        }

    
    }
}
